package com.hu.bgp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.hu.bgp.client.gin.BgpGinModule;
import com.hu.bgp.client.gin.BgpGinjector;
import com.hu.bgp.client.rpc.BgpAdminService;
import com.hu.bgp.client.rpc.BgpAdminServiceAsync;
import com.hu.bgp.client.rpc.BgpService;
import com.hu.bgp.client.rpc.BgpServiceAsync;
import com.hu.bgp.client.widgets.uicomponents.GameTypeTableAdmin;
import com.hu.bgp.client.widgets.uicomponents.GameTypeTableUser;

import com.hu.bgp.shared.data.ClientGameType;
import com.hu.bgp.shared.data.ClientGameTypeCollection;

/**
 * De normale GUI voor het selecteren van een spelletje.
 * 
 * @author ejvos
 */
public class PlayerClient implements EntryPoint
{

	private BgpServiceAsync bgpService;
	private BgpAdminServiceAsync bgpAdminService = GWT
			.create(BgpAdminService.class);
	private final Button startButton = new Button("Start selected game type");
	private final Anchor adminButton = new Anchor("Admin");
	private final Button loadButton = new Button("Load selected game type");
	private final Button unloadButton = new Button("Unload selected game type");
	private final GameTypeTableUser gameTypeTable = new GameTypeTableUser();
	private final GameTypeTableAdmin gameTypeTableAdmin = new GameTypeTableAdmin();

	public void onModuleLoad()
	{

		// get a binding to the servlet
		bgpService = GWT.create(BgpService.class);
		ServiceDefTarget target = (ServiceDefTarget) bgpService;
		// zie ook BgpConfiguration.getPlayerClientUrlPrefix
		target.setServiceEntryPoint("/bgp/bgpService");

		// DEBUG load BgpAppUI instead of old ui and a run async test
		if (RootPanel.get("gameTypeTable") == null) {
			GWT.runAsync(new RunAsyncCallback() {

				public void onFailure(Throwable reason)
				{
					// TODO Auto-generated method stub

				}

				public void onSuccess()
				{
					
					new AppController().go(RootPanel.get("bgpapp"));

				}

			});
			return;
		}

		bgpService.login("not yet implemented", "just to created the session",
				new AsyncCallback<Void>() {

					public void onFailure(Throwable caught)
					{
						// TODO Auto-generated method stub

					}

					public void onSuccess(Void result)
					{
						// TODO Auto-generated method stub

					}

				});

		loadadminEntryies();
		// grab a collection of gametypes
		bgpService
				.getClientGameTypeCollection(new AsyncCallback<ClientGameTypeCollection>() {
					public void onSuccess(ClientGameTypeCollection result)
					{
						for (ClientGameType gameType : result) {
							gameTypeTable.addGameRow(gameType);
						}
					}

					public void onFailure(Throwable caught)
					{
						Window.alert("failed to get Game types");

					}
				});

		loadButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event)
			{
				// TODO Auto-generated method stub
				bgpAdminService.activateGame(gameTypeTableAdmin.getSelection(),
						new AsyncCallback<Void>() {

							public void onFailure(Throwable caught)
							{
								// TODO Auto-generated method stub

							}

							public void onSuccess(Void result)
							{
								// TODO Auto-generated method stub
								// loadadminEntryies();
								Window.alert("Succes!");
							}

						});
			}

		});
		startButton.addClickHandler(new StartHandler());
		adminButton.addClickHandler(new AdminHandler());
		RootPanel.get("gameTypeTable").add(gameTypeTable);
		RootPanel.get("actionButtons").add(startButton);
		RootPanel.get("adminButton").add(adminButton);
	}

	private class StartHandler implements ClickHandler
	{
		public void onClick(ClickEvent event)
		{
			// FIXME
			// TODO
			new GameLoader().loadGame(gameTypeTable.getSelection());
		}

	}

	public static native void push(String s) /*-{
		$wnd.RawGWT = s;
	}-*/;

	private class AdminHandler implements ClickHandler
	{
		public void onClick(ClickEvent event)
		{
			RootPanel gameTypeTable = RootPanel.get("gameTypeTable");
			RootPanel actionButtons = RootPanel.get("actionButtons");
			gameTypeTable.getElement().setInnerText("");
			actionButtons.getElement().setInnerText("");
			gameTypeTable.add(gameTypeTableAdmin);

			loadButton.setStyleName("displayInline");
			unloadButton.setStyleName("displayInline");
			actionButtons.add(loadButton);
			actionButtons.add(unloadButton);

		}
	}

	// TODO QUICK DIRTY HACK
	private void loadadminEntryies()
	{
		ServiceDefTarget target = (ServiceDefTarget) bgpAdminService;
		// zie ook BgpConfiguration.getPlayerClientUrlPrefix
		target.setServiceEntryPoint("/bgp/admin");

		bgpAdminService
				.getClientGameTypeCollection(new AsyncCallback<ClientGameTypeCollection>() {
					public void onSuccess(ClientGameTypeCollection result)
					{
						for (ClientGameType gameType : result) {
							gameTypeTableAdmin.addGameRow(gameType);
						}
					}

					public void onFailure(Throwable caught)
					{
						caught.printStackTrace();

					}
				});
	}

}
